package speed2010.matcher.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import speed2010.matcher.util.OntologyMatcherProperties;

import speed2010.matcher.gui.FAbout;
import speed2010.matcher.gui.FOntologyMatcherGUI;
import speed2010.matcher.gui.FOptions;
import speed2010.matcher.gui.panels.MainPanel;
import speed2010.matcher.semanticmatching.basics.ESSMFunction;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FOntologyMatcherGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JMenu jMenu1;
	private JMenuItem mniAbout;
	private JMenu mniSaveTo;
	private JRadioButtonMenuItem mniJaccard;
	private JRadioButtonMenuItem mniDICE;
	private JRadioButtonMenuItem mniAverage;
	private JMenu mnuFunction;
	private JSeparator sepExecute;
	private JMenuItem mniSSM;
	private JMenuItem mniDB;
	private JMenuItem mniExit;
	private JMenu mnuExecute;
	private JSeparator mnsExit;
	private JMenuItem mniLog;
	private JSeparator mnsLog;
	private JMenuItem mniClean;
	private JMenuItem mniPreferences;
	private JMenuItem mniOWL;
	private JMenu mnuAbout;
	private JMenuBar mnuMain;

	private JPanel jContentPane = null;
	public static FOntologyMatcherGUI INSTANCE = null;
	
	
	/**
	 * This is the default constructor
	 */
	public FOntologyMatcherGUI() {
		super();
		initialize();
		if(INSTANCE == null)
			INSTANCE = this;
		
		ESSMFunction function = OntologyMatcherProperties.SSM_FUNCTION;
		
		if(function == ESSMFunction.Average)
		{
			getMniAverage().setSelected(true);
		}
		else if(function == ESSMFunction.DICE)
		{
			getMniDICE().setSelected(true);
		}
		else if(function == ESSMFunction.Overlap)
		{
			getMniJaccard().setSelected(true);
		}		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			//UIManager.setLookAndFeel(new NimbusLookAndFeel());
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setSize(900, 600);
		this.setContentPane(getJContentPane());
		this.setTitle("SemMatcher");
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setMaximumSize(new java.awt.Dimension(0, 0));
		this.setLocationRelativeTo(null);
		this.setJMenuBar(getMnuMain());
		this.setResizable(true);
		//this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("speed/ontologymatcher/gui/images/emulator.ico")).getImage());
		this.setMinimumSize(new Dimension(700, 500));
		
		
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("img/semMatcherICO.PNG")); 
		ButtonGroup bgroup = new ButtonGroup();
		bgroup.add(getMniAverage());
		bgroup.add(getMniDICE());
		bgroup.add(getMniJaccard());
	}
	
	protected ImageIcon createImageIcon(String path,
            String description) {
		java.net.URL imgURL = getClass().getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL, description);
		} else {
		System.err.println("Couldn't find file: " + path);
			return null;
		}
	}


	private JMenuBar getMnuMain()
	{
		if(mnuMain == null)
		{
			mnuMain = new JMenuBar();
			mnuMain.add(getJMenu1());
			mnuMain.add(getMnuExecute());
			mnuMain.add(getMnuAbout());			
		}
		
		return mnuMain;
	}
	
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new MainPanel();
			jContentPane.setPreferredSize(new java.awt.Dimension(794, 564));
		}
		return jContentPane;
	}
	
	private JMenu getJMenu1() {
		if(jMenu1 == null) {
			jMenu1 = new JMenu();
			jMenu1.setText("File");
			jMenu1.add(getMniSaveTo());
			jMenu1.add(getMniPreferences());
			jMenu1.add(getMniClean());
			jMenu1.add(getMnsExit());
			jMenu1.add(getMniExit());		
			
		}
		return jMenu1;
	}
	
	private JMenu getMnuAbout() {
		if(mnuAbout == null) {
			mnuAbout = new JMenu();
			mnuAbout.setText("Help");
			mnuAbout.add(getMniAbout());

		}
		return mnuAbout;
	}
	
	private JMenuItem getMniAbout() {
		if(mniAbout == null) {
			mniAbout = new JMenuItem();
			mniAbout.setText("About");
			mniAbout.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JDialog dialog = new FAbout(INSTANCE);
					dialog.setVisible(true);
				}
			});
		}
		return mniAbout;
	}
	
	private JMenu getMniSaveTo() {
		if(mniSaveTo == null) {
			mniSaveTo = new JMenu();
			mniSaveTo.setText("Save to...");
			mniSaveTo.add(getMniOWL());
			mniSaveTo.add(getMniDB());
		}
		return mniSaveTo;
	}
	
	private JMenuItem getMniOWL() {
		if(mniOWL == null) {
			mniOWL = new JMenuItem();
			mniOWL.setText("OWL");
			mniOWL.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {					
					getMainPanel().saveOWL();
				}
			});
		}
		return mniOWL;
	}
	
	private JMenuItem getMniDB() {
		if(mniDB == null) {
			mniDB = new JMenuItem();
			mniDB.setText("Database");
			mniDB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getMainPanel().saveToDB();
				}
			});
		}
		return mniDB;
	}
	
	private JMenuItem getMniPreferences() {
		if(mniPreferences == null) {
			mniPreferences = new JMenuItem();
			mniPreferences.setText("Preferences");
			mniPreferences.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					FOptions options = new FOptions(INSTANCE);					
					JDialog dialog = options;
					dialog.setVisible(true);
				}
			});
		}
		return mniPreferences;
	}
	
	private JMenuItem getMniExit() {
		if(mniExit == null) {
			mniExit = new JMenuItem();
			mniExit.setText("Exit");
			mniExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return mniExit;
	}
	
	private JMenu getMnuExecute() {
		if(mnuExecute == null) {
			mnuExecute = new JMenu();
			mnuExecute.setText("Matching");
			mnuExecute.add(getMniSSM());
			mnuExecute.add(getSepExecute());
			mnuExecute.add(getMnuFunction());
			mnuExecute.add(getMnsLog());
			mnuExecute.add(getMniLog());
		}
		return mnuExecute;
	}

	private JMenuItem getMniSSM() {
		if(mniSSM == null) {
			mniSSM = new JMenuItem();
			mniSSM.setText("Run...");
			mniSSM.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {					
					getMainPanel().openRunDialog();
				}
			});
		}
		return mniSSM;
	}
	
	public MainPanel getMainPanel()
	{
		MainPanel mp = null;
		Container panel = getContentPane();

		if(panel instanceof MainPanel)
		{
			mp = ((MainPanel)panel);
		}	
		return mp;
	}
	
	private JSeparator getSepExecute() {
		if(sepExecute == null) {
			sepExecute = new JSeparator();
		}
		return sepExecute;
	}
	
	private JMenu getMnuFunction() {
		if(mnuFunction == null) {
			mnuFunction = new JMenu();
			mnuFunction.setText("Function");
			mnuFunction.add(getMniAverage());
			mnuFunction.add(getMniDICE());
			mnuFunction.add(getMniJaccard());
		}
		return mnuFunction;
	}
	
	private JRadioButtonMenuItem getMniAverage() {
		if(mniAverage == null) {
			mniAverage = new JRadioButtonMenuItem();
			mniAverage.setText("Average");
			mniAverage.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					saveFunctionType(ESSMFunction.Average);
				}
			});
		}
		return mniAverage;
	}
	
	private JRadioButtonMenuItem getMniDICE() {
		if(mniDICE == null) {
			mniDICE = new JRadioButtonMenuItem();
			mniDICE.setText("DICE");
			mniDICE.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					saveFunctionType(ESSMFunction.DICE);
				}
			});
		}
		return mniDICE;
	}
	
	private JRadioButtonMenuItem getMniJaccard() {
		if(mniJaccard == null) {
			mniJaccard = new JRadioButtonMenuItem();
			mniJaccard.setText("Overlap");
			mniJaccard.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					saveFunctionType(ESSMFunction.Overlap);
				}
			});
		}
		return mniJaccard;
	}
	
	private void saveFunctionType(ESSMFunction function)
	{
		getMainPanel().setEnabled(false);
		OntologyMatcherProperties.saveOption("ssm_function", function.toString());
		getMainPanel().setEnabled(true);
	}
	
	private JSeparator getMnsExit() {
		if(mnsExit == null) {
			mnsExit = new JSeparator();
		}
		return mnsExit;
	}
	
	private JMenuItem getMniClean() {
		if(mniClean == null) {
			mniClean = new JMenuItem();
			mniClean.setText("Clean results");
			mniClean.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getMainPanel().clean();
				}
			});
		}
		return mniClean;
	}
	
	private JSeparator getMnsLog() {
		if(mnsLog == null) {
			mnsLog = new JSeparator();
		}
		return mnsLog;
	}
	
	private JMenuItem getMniLog() {
		if(mniLog == null) {
			mniLog = new JMenuItem();
			mniLog.setText("Operations Log");
			mniLog.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {					
					getMainPanel().generateLog();
				}
			});
		}
		return mniLog;
	}

}
